// pages/my_order/my_order.js


const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currtab: 0,
    swipertab: [{ name: '待支付', index: 0 }, { name: '待出行', index: 1 }, { name: '已完成', index: 2 }, { name: '已退款', index: 3 }],
    waitPay: [],
    overPay: [],
    overOrder: [],
    refundOrder: [],
    heightScreen: ''
  },
  tabSwitch: function (e) {
    var that = this
    if (this.data.currtab == e.target.dataset.current) {
      return false
    } else {
      that.setData({
        currtab: e.target.dataset.current
      })
    }
  },

  tabChange: function (e) {
    this.setData({ currtab: e.detail.current })
  },

  // orderShow:function(){
  //   let that=this
  //   switch(this.data.currtab){
  //     case 0:
  //       that.waitPayShow()
  //       break
  //     case 1:
  //       that.completingShow()
  //       break
  //     case 2:
  //       that.alreadyShow()
  //       break
  //     case 3:
  //       that.cancelShow()
  //       break
  //   }
  // },
  waitPayShow: function () {
    this.setData({
      waitPayOrder: [{ state: "待支付", status: "未开始", name: app.globalData.myorderlist.travelInfo, time: app.globalData.ordertime, url: "https://img1.baidu.com/it/u=3515749756,3806796580&fm=26&fmt=auto&gp=0.jpg", money: app.globalData.myorderlist.price }]
    })
  },
  // completingShow:function(){
  //   this.setData({
  //     completingOrder:[{name:app.globalData.myorderlist.travelInfo,state:"正在支付",time:app.globalData.ordertime,status:"未开始",url:"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp3.ssl.qhimgs3.com%2Ft01f8a93f273d4ef20d.jpg&refer=http%3A%2F%2Fp3.ssl.qhimgs3.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1628297976&t=57c7af8d3b0abaf89b5f507892acd1d1",money:app.globalData.myorderlist.price}],
  //   })
  // },
  // alreadyShow:function(){
  //   this.setData({
  //     alreadyOrder:[{name:app.globalData.myorderlist.travelInfo,state:"已完成",time:app.globalData.ordertime,status:"已结束",url:"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.029558.com%2Fupload_file%2Ftravelroute%2F5b2ebca75332006683e6e5bd032b6934.png&refer=http%3A%2F%2Fwww.029558.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1628298247&t=93eaf7147f605f3fee06f40a13308345",money:app.globalData.myorderlist.price}],
  //   })
  // },
  // cancelShow:function(){
  //   this.setData({
  //     cancelOrder:[{name:app.globalData.myorderlist.travelInfo,state:"已取消",time:app.globalData.ordertime,status:"未开始",url:"https://img1.baidu.com/it/u=3515749756,3806796580&fm=26&fmt=auto&gp=0.jpg",money:app.globalData.myorderlist.price}],
  //   })
  // },

  getup: function (e) {
    var that = this
    wx.showModal({
      content: '请确认支付',
      success(arg) {
        if (arg.confirm) {
          if (that.data.currtab < 1) {
            that.setData({ currtab: that.data.currtab + 1 })
          } else if (that.data.currtab == 1) {
            wx.showLoading({
              title: '支付成功',
            });
            setTimeout(function () {
              //页面跳转相当于
              wx.hideLoading({
                success: (res) => { },
              })
              wx.switchTab({
                url: '../home/home',
              })
            }, 2000);
          }
        }
      }
    })

  },
  getdownl: function (e) {
    var that = this
    wx.showModal({
      content: '是否放弃支付？',
      success(res) {
        if (res.confirm) {
          if (that.data.currtab != 3) {
            that.setData({ currtab: 3 })
            wx.showLoading({
              title: '支付失败',
            });
            setTimeout(function () {

              //页面跳转相当于
              wx.hideLoading({
                success: (res) => { },
              })
              wx.switchTab({
                url: '../home/home',
              })
            }, 2000);
          }
        }
      }
    })
  },

  delOrder: function (event) {
    var that = this;
    var orderId = event.currentTarget.dataset.orderid;
    var lod = event.currentTarget.dataset.lod;
    var suc = event.currentTarget.dataset.suc;
    var err = event.currentTarget.dataset.err;
    var content = event.currentTarget.dataset.content;
    wx.showModal({
      title: '提示',
      content: content,
      success: function (res) {
        // console.log(res)
        if (res.confirm) {
          wx.showLoading({
            title: lod,
          });
          wx.request({
            url: 'http://localhost:8180/webapps/DelOrderServlet?orderId=' + orderId,
            method: 'GET',
            success: function (res) {
              // console.log(res)
              wx.hideLoading({
                success: (res) => { },
              })
              if (res.data.data == false) {
                wx.showToast({
                  title: err,
                  icon: 'error'
                })
                return;
              }
              that.setData({
                waitPay: [],
                overPay: [],
                overOrder: [],
                refundOrder: []
              })
              that.showAllOrder();
              wx.showToast({
                title: suc,
                icon: 'success'
              })
            }
          })
        } else if (res.cancel) {
          wx.showToast({
            title: '已取消',
            icon:'none'
          })
        } else {
          wx.showToast({
            title: '异常请求',
            icon: 'error'
          })
        }
      }
    })
  },
  payOrder: function (event) {
    // console.log(event)
    // console.log(event.currentTarget.dataset)
    var orderId = event.currentTarget.dataset.orderid;
    var lod = event.currentTarget.dataset.lod;
    var suc = event.currentTarget.dataset.suc;
    var err = event.currentTarget.dataset.err;
    var stater = event.currentTarget.dataset.stater;
    var content = event.currentTarget.dataset.content;
    var that = this;

    wx.showModal({
      title: '提示',
      content: content,
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: lod,
          });
          wx.request({
            url: 'http://localhost:8180/webapps/FixOrderServlet?stater=' + stater + '&orderId=' +
              orderId,
            method: 'GET',
            success: function (res) {
              wx.hideLoading({
                success: (res) => { },
              })
              // console.log(res)
              if (res.data.data = false) {
                wx.showToast({
                  title: err,
                  icon: 'error'
                })
                return;
              }
              that.setData({
                waitPay: [],
                overPay: [],
                overOrder: [],
                refundOrder: []
              })
              that.showAllOrder();
              wx.showToast({
                title: suc,
                icon: 'success'
              })
      
            },
            fail: function () {
              wx.hideLoading({
                success: (res) => { },
              })
              wx.showToast({
                title: '连接服务器失败',
                icon: 'error'
              })
            }
          })
        } else if (res.cancel) {
          wx.showToast({
            title: '已取消',
            icon:'none'
          })
        } else {
          wx.showToast({
            title: '异常请求',
            icon: 'error'
          })
        }
      }
    })
  },
  showAllOrder: function () {
    var that = this;
    //获取屏幕高度
    that.setData({
      heightScreen: wx.getSystemInfoSync().windowHeight
    })
    // console.log(that.data.heightScreen)
    wx.showLoading({
      title: '正在加载',
    });
    var userId = wx.getStorageSync('userid')
    wx.request({
      url: 'http://localhost:8180/webapps/SelectOrderServlet?userId=' + userId,
      method: 'GET',
      success: function (res) {
        // console.log(res)
        if (res.data.error == 'null') {
          wx.hideLoading({
            success: (res) => { },
          })
          wx.showToast({
            title: '暂无订单',
            icon: 'error'
          })
          return;
        }
        for (var i = 0; i < res.data.data.length; i++) {
          if (res.data.data[i].orderStater == 0) {
            that.setData({
              waitPay: that.data.waitPay.concat(res.data.data[i])
            })
            continue;
          }
          if (res.data.data[i].orderStater == 1) {
            that.setData({
              overPay: that.data.overPay.concat(res.data.data[i])
            })
            continue;
          }
          if (res.data.data[i].orderStater == 2) {
            that.setData({
              overOrder: that.data.overOrder.concat(res.data.data[i])
            })
            continue;
          }
          if (res.data.data[i].orderStater == 3) {
            that.setData({
              refundOrder: that.data.refundOrder.concat(res.data.data[i])
            })
            continue;
          }
        }
        // console.log(that.data.waitPay)
        // console.log(that.data.overOrder)
        wx.hideLoading({
          success: (res) => { },
        })
      },
      fail: function () {
        wx.hideLoading({
          success: (res) => { },
        })
        wx.showToast({
          title: '连接服务器失败',
          icon: 'error'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.showAllOrder()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getDeviceInfo()
  },
  getDeviceInfo: function () {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          deviceW: res.windowWidth,
          deviceH: res.windowHeigh
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})